<template>
  <div class="open-client">
    <img :src="imgSrc" alt="" style="width: 200px; height: 160px" />
    <span class="prompt">已安装客户端，请点击下方按钮打开客户端</span>
    <Button type="primary" :danger="true" @click="getUrl">打开客户端</Button>
    <span class="download-text"
      >尚未安装客户端？
      <Button type="primary" @click="toDownload">下载并安装</Button>
    </span>
  </div>

  <Modal v-model:visible="visible" title="尚未安装客户端" class="confirm-dialog" width="200">
    <p>未安装客户端，请点击下载并安装按钮，进行客户端安装</p>
    <template #footer>
      <Button type="danger" @click="handleConfirm">确定</Button>
    </template>
  </Modal>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { Button, Modal } from 'ant-design-vue';
import { openUriWithTimeoutHack } from '/@/utils/monitorGoogle';
import { getMonoAppExeUrl, getMonoAppUrl } from '/@/api/integrated-center/open-client/openClient';
import { MonoAppExeUrlParams, MonoAppUrlParams } from '/@/api/integrated-center/model/openClientModel';

const imgSrc = new URL('../../../../src/assets/images/integrated/mindmaster.jpg', import.meta.url).href;
const windowUrl = window.location.href;
const visible = ref(false);

let urlQueryParams: MonoAppUrlParams;

function getUrlQuery(url) {
  let tempArr = url.split('?')[1];
  if (tempArr) {
    tempArr = tempArr.split('&');
    let queryObj = {};
    tempArr.forEach((element) => {
      let k = element.split('=')[0];
      let v = element.split('=')[1];
      queryObj[k] = v;
    });
    return queryObj;
  }
}

function openApp(url) {
  openUriWithTimeoutHack(url, () => {
    visible.value = true;
  });
}

const getUrl = () => {
  getMonoAppUrl(urlQueryParams).then((res) => {
    openApp(res);
  });
};

const toDownload = () => {
  let params: MonoAppExeUrlParams = {
    tenantCode: urlQueryParams.tenantCode,
    appId: urlQueryParams.appId,
  };

  getMonoAppExeUrl(params).then((res) => {
    window.location.href = res;
  });
};

function handleConfirm() {
  visible.value = false;
}

urlQueryParams = getUrlQuery(windowUrl);
</script>

<style lang="less" scoped>
.open-client {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  img {
    width: 210px;
    height: 40px;
  }
  .prompt {
    margin: 36px 0 24px 0;
  }
  .download-text {
    margin-top: 160px;
  }
}
.confirm-dialog {
  font-size: 12px;
  font-family: Microsoft YaHei, Microsoft YaHei-Regular;
  color: #575d6c;
}
</style>
